import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, OnInit, ViewChild, Input } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/common/app-component-base';
import {  TaskServiceProxy, WorkflowServiceProxy, OutsourcingWorkflowServiceProxy, UserServiceProxy, ProcessingDeliveryDto, ProcessingDeliveryViewDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';

@Component({
    selector: 'ProcessingDeliveryAudit',
    templateUrl: './ProcessingDeliveryAudit.component.html',
    styleUrls: ['./ProcessingDeliveryAudit.component.css']
})
export class ProcessingDeliveryAuditComponent extends AppComponentBase implements OnInit {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('taskdataTable') taskdataTable: Table;
    @ViewChild('dataTable') dataTable: Table;

    @Input() workflowid: any;
    @Output() out = new EventEmitter();

    active = false;
    saving = false;
    _processingDeliveryDto: ProcessingDeliveryDto = new ProcessingDeliveryDto();
    _processingDeliveryViewDto: ProcessingDeliveryViewDto[];
    detailsprimengTableHelper: PrimengTableHelper;
    ngOnInit(): void {

        console.log(this.workflowid);
        this.initForm();
    }
    constructor(injector: Injector,
        private route: ActivatedRoute,
        private _OutsourcingWorkFlowServiceProxy: OutsourcingWorkflowServiceProxy,
        private _taskServiceProxy: TaskServiceProxy,
        private _userServiceProxy: UserServiceProxy,
        private _workflowService: WorkflowServiceProxy) {
        super(injector);
        this.detailsprimengTableHelper = new PrimengTableHelper();

    }

    initForm(): void {
        this._OutsourcingWorkFlowServiceProxy.getProcessingDeliveryByWorkFlowId(this.workflowid).subscribe(dto => {
            this._processingDeliveryDto = dto.processingDeliveryDto;
            this._processingDeliveryViewDto = dto.processingDeliveryViewDtos;
            console.log(dto);
            this.primengTableHelper.totalRecordsCount = this._processingDeliveryViewDto.length;
            this.primengTableHelper.records = this._processingDeliveryViewDto;
            this.primengTableHelper.hideLoadingIndicator();

            _.forEach(this._processingDeliveryViewDto, viewItem => {
                console.log(viewItem.taskForModelId);
                this._taskServiceProxy.getTaskForEdit(viewItem.taskForModelId)
                    .subscribe(userResult => {
                        console.log(userResult);
                        viewItem.taskCode = userResult.taskCode;
                        viewItem.taskName = userResult.drawingName;
                        viewItem.picNo = userResult.drawingCode;
                    });

                this.active = true;
            });
        });
    }

    audityes(): void {
        this._OutsourcingWorkFlowServiceProxy.processingDeliveryAuditFlow_execute(this.workflowid, 'yes').subscribe(dto => {
            this.notify.info('提交成功！');
            this.out.emit(dto);
        });
    }

    auditno(): void {
        this._OutsourcingWorkFlowServiceProxy.processingDeliveryAuditFlow_execute(this.workflowid, 'no').subscribe(dto => {
            this.notify.info('提交成功！');
            this.out.emit(dto);
        });
    }
}
